﻿Imports BrainWorklite.BussinesLogic
Public Class LabeledTexbox
    Private _currentProperty As BrainWorklite.Entity.Attributes.PropertiesAttributes

    Private Enum controlType
        text
        calendar
        Combo
    End Enum
    Private ctlType As controlType
    Private WithEvents cal As DateTimePicker
    Private WithEvents cmb As ComboBox
    Public Sub New(ByVal prop As BrainWorklite.Entity.Attributes.PropertiesAttributes)
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        _currentProperty = prop
        Me.Name = prop.PropertyName
        Me.Value = prop.PropertyValue
        Me.Label1.Text = prop.LabelAtt.Text

        If prop.PropertyType.Name.ToLower.Contains("date") Then
            cal = New DateTimePicker()
            cal.Location = Me.TextBox1.Location
            cal.Format = DateTimePickerFormat.Short

            cal.Size = _
            Me.TextBox1.Size

            Me.Controls.Add(cal)

            Me.TextBox1.Visible = False
            ctlType = controlType.calendar
        Else
            If prop.isCollectionAtt IsNot Nothing Then
                cmb = New ComboBox()
                cmb.Location = Me.TextBox1.Location
                cmb.Size = Me.TextBox1.Size
                Me.Controls.Add(cmb)
                Me.TextBox1.Visible = False
                ctlType = controlType.Combo


                Dim bl = BussinesLogicGenericHelper.getBussinesLogicGeneric(prop.isCollectionAtt.ForeingTableName)
                Dim lista = bl.getList()
                cmb.DataSource = lista
                cmb.ValueMember = Entity.EntityBase.DataValueFieldName
                cmb.DisplayMember = Entity.EntityBase.DataTextFieldName
            End If
        End If

    End Sub

    Public Property Value() As Object
        Get
            Select Case ctlType
                Case controlType.calendar
                    Return Me.cal.Value
                Case Else
                    Return Me.TextBox1.Text
            End Select

        End Get
        Set(ByVal value As Object)
            Select Case ctlType
                Case controlType.calendar
                    If value IsNot Nothing Then

                        'If DirectCast(value, DateTime) <> Date.MinValue Then
                        Me.cal.Value = Date.Parse(value)
                        'End If


                    End If
                Case controlType.Combo
                    If value IsNot Nothing Then
                        For i As Integer = 0 To cmb.Items.Count - 1
                            If cmb.Items(0).ToString() = value.ToString() Then
                                cmb.SelectedIndex = i
                                Exit For
                            End If
                        Next
                    End If
                Case Else
                    Me.TextBox1.Text = value
            End Select

        End Set
    End Property

    Public Overrides Property Text() As String
        Get
            Return Value
        End Get
        Set(ByVal value As String)
            Me.Value = value
        End Set
    End Property

    Public ReadOnly Property CurrentProperty() As BrainWorklite.Entity.Attributes.PropertiesAttributes
        Get
            Return _currentProperty
        End Get

    End Property



    Public ReadOnly Property Textbox() As TextBox
        Get
            Return Me.TextBox1
        End Get
    End Property
    Public ReadOnly Property Label() As Label
        Get
            Return Me.Label1
        End Get
    End Property


    Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, cal.ValueChanged

        Select Case ctlType
            Case controlType.calendar
                _currentProperty.PropertyValue = Me.cal.Value
            Case Else
                _currentProperty.PropertyValue = Me.TextBox1.Text
        End Select
    End Sub


End Class
